﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


namespace DragDraw.Service
{
    public class WidgetLibInfoData : SqlDataManager
    {
        ///<summary>
        ///插入小工具库信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public int Insert(WidgetLibInfo widgetLibInfo)//what if the id exist?
        {
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.StoredProcedure;
            curCommand.CommandText = "InsertWidgetLibInfo";
            curCommand.Parameters.Add("@result", SqlDbType.Int);
            curCommand.Parameters.Add("@id", SqlDbType.Int);
            curCommand.Parameters.Add("@widgetName", SqlDbType.NVarChar,50);
            curCommand.Parameters.Add("@widgetFileName", SqlDbType.NVarChar,50);
            curCommand.Parameters.Add("@widgetAssemblyName", SqlDbType.NVarChar,50);
            curCommand.Parameters.Add("@tags", SqlDbType.NVarChar,4000);
            curCommand.Parameters.Add("@imageUrl", SqlDbType.NVarChar,200);
            curCommand.Parameters.Add("@description", SqlDbType.NVarChar,4000);
            curCommand.Parameters["@result"].Direction = ParameterDirection.Output;
            curCommand.Parameters["@id"].Direction = ParameterDirection.Output;
            curCommand.Parameters["@widgetName"].Value = widgetLibInfo.WidgetName;
            curCommand.Parameters["@widgetFileName"].Value = widgetLibInfo.WidgetFileName;
            curCommand.Parameters["@widgetAssemblyName"].Value = widgetLibInfo.WidgetAssemblyName;
            curCommand.Parameters["@tags"].Value = widgetLibInfo.Tags;
            curCommand.Parameters["@imageUrl"].Value = widgetLibInfo.ImageUrl;
            curCommand.Parameters["@description"].Value = widgetLibInfo.Description;

            curCommand.Connection = SqlDataManager.OpenConnection();
            int result = 0;
            try
            {
                result = curCommand.ExecuteNonQuery();
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("WidgetLibInfoData.Insert:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (Convert.ToInt32(curCommand.Parameters["@result"].Value) == 0)//有没有执行sql语句？
            {
                throw new DataErrorException("WidgetLibInfoData.Insert:没有执行sql语句！", null);
            }
            if (result <= 0)//执行的sql语句是否成功？
            {
                throw new DataErrorException("WidgetLibInfoData.Insert:执行了sql语句，可是sql语句没有影响任何一条记录！", null);
            }
            return Convert.ToInt32(curCommand.Parameters["@id"].Value);
        }
        ///<summary>
        ///删除小工具库信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public bool Delete(int id)
        {
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.StoredProcedure;
            curCommand.CommandText = "DeleteWidgetLibInfo";
            curCommand.Parameters.Add("@result", SqlDbType.Int);
            curCommand.Parameters.Add("@id", SqlDbType.Int);
            curCommand.Parameters["@result"].Direction = ParameterDirection.Output;
            curCommand.Parameters["@id"].Value = id;

            curCommand.Connection = SqlDataManager.OpenConnection();
            int result = 0;
            try
            {
                result = curCommand.ExecuteNonQuery();
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("WidgetLibInfoData.Delete:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (Convert.ToInt32(curCommand.Parameters["@result"].Value) == 0)//有没有执行sql语句？
            {
                throw new DataErrorException("WidgetLibInfoData.Delete:没有执行sql语句！", null);
            }
            if (result <= 0)//执行的sql语句是否成功？
            {
                throw new DataErrorException("WidgetLibInfoData.Delete:执行了sql语句，可是sql语句没有影响任何一条记录！", null);
            }
            return true;
        }
        ///<summary>
        ///更新小工具库信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public bool Update(WidgetLibInfo widgetLibInfo)
        {
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.StoredProcedure;
            curCommand.CommandText = "UpdateWidgetLibInfo";
            curCommand.Parameters.Add("@result", SqlDbType.Int);
            curCommand.Parameters.Add("@id", SqlDbType.Int);
            curCommand.Parameters.Add("@widgetName", SqlDbType.NVarChar, 50);
            curCommand.Parameters.Add("@widgetFileName", SqlDbType.NVarChar, 50);
            curCommand.Parameters.Add("@widgetAssemblyName", SqlDbType.NVarChar, 50);
            curCommand.Parameters.Add("@tags", SqlDbType.NVarChar, 4000);
            curCommand.Parameters.Add("@imageUrl", SqlDbType.NVarChar, 200);
            curCommand.Parameters.Add("@description", SqlDbType.NVarChar, 4000);
            curCommand.Parameters["@result"].Direction = ParameterDirection.Output;
            curCommand.Parameters["@id"].Value = widgetLibInfo.ID;
            curCommand.Parameters["@widgetName"].Value = widgetLibInfo.WidgetName;
            curCommand.Parameters["@widgetFileName"].Value = widgetLibInfo.WidgetFileName;
            curCommand.Parameters["@widgetAssemblyName"].Value = widgetLibInfo.WidgetAssemblyName;
            curCommand.Parameters["@tags"].Value = widgetLibInfo.Tags;
            curCommand.Parameters["@imageUrl"].Value = widgetLibInfo.ImageUrl;
            curCommand.Parameters["@description"].Value = widgetLibInfo.Description;

            curCommand.Connection = SqlDataManager.OpenConnection();
            int result = 0;
            try
            {
                result = curCommand.ExecuteNonQuery();
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("WidgetLibInfoData.Update:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (Convert.ToInt32(curCommand.Parameters["@result"].Value) == 0)//有没有执行sql语句？
            {
                throw new DataErrorException("WidgetLibInfoData.Update:没有执行sql语句！", null);
            }
            if (result <= 0)//执行的sql语句是否成功？
            {
                throw new DataErrorException("WidgetLibInfoData.Update:执行了sql语句，可是sql语句没有影响任何一条记录！", null);
            }
            return true;
        }
        ///<summary>
        ///查询小工具库信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public List<WidgetLibInfo> Query()//if does not exist,return null
        {
            List<WidgetLibInfo> result_widgetLibInfoList = new List<WidgetLibInfo>();
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.Text;
            curCommand.CommandText = "select * from WidgetLibInfo";

            curCommand.Connection = SqlDataManager.OpenConnection();
            bool HasResult = false;
            try
            {
                SqlDataReader reader = curCommand.ExecuteReader(CommandBehavior.Default);
                if (reader.HasRows)
                {
                    HasResult = true;
                    while (reader.Read())
                    {
                        WidgetLibInfo temp_widgetLibInfo = new WidgetLibInfo();
                        temp_widgetLibInfo.ID = reader.GetInt32(0);
                        temp_widgetLibInfo.WidgetName = reader.GetString(1);
                        temp_widgetLibInfo.WidgetFileName = reader.GetString(2);
                        temp_widgetLibInfo.WidgetAssemblyName = reader.GetString(3);
                        temp_widgetLibInfo.Tags = reader.GetString(4);
                        temp_widgetLibInfo.ImageUrl = reader.GetString(5);
                        temp_widgetLibInfo.Description = reader.GetString(6);
                        result_widgetLibInfoList.Add(temp_widgetLibInfo);
                    }
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                ErrorManager.Instance.LogMessage(error.Message);
                throw new DataErrorException("WidgetLibInfoData.Query:执行SqlCommand的时候发生错误！", error);
                
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (!HasResult)
            {
                throw new DataNotFoundException("WidgetLibInfoData.Query:顺利执行完sql语句，可是没有找到匹配的记录！", null);
            }
            return result_widgetLibInfoList;
        }
        ///<summary>
        ///显示所有小工具库信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public void Show()
        {
            Console.WriteLine("-----WidgetLibInfo Table-----------------");
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.Text;
            curCommand.CommandText = "select * from WidgetLibInfo";
            curCommand.Connection = SqlDataManager.OpenConnection();
            try
            {
                SqlDataReader reader = curCommand.ExecuteReader(CommandBehavior.Default);
                while (reader.Read())
                {
                    Console.WriteLine("{0} {1} {2} {3} {4} {5} {6}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetString(5), reader.GetString(6));
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("WidgetLibInfoData.Show:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
                Console.WriteLine("-------------------------------------");
            }
        }
    }
}